define(["angularAMD", 'moment'], function (angularAMD, moment) {
    angularAMD.service('$RollbackService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var service = this;

            service.init = function(options){

                //還原資訊取得
                function rollbackInfoGet(content, callback){
                    // content = {machine_no: "A設備編號", op_no: "A作業站", plot_no: "MO904251-001",  status: "1"};
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_get',
                        content : content,
                        msg : 'mobile05.msg.rollback_info_get',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                //報工還原
                function rollbackInfoProcess(content, callback){
                    // $rootScope.showToast('報工還原成功');
                    // if(callback)
                    //     callback();
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.rollback_info_process',
                        content : content,
                        msg : 'mobile05.msg.rollback_info_process',
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                function beforeOpenDialog(options, callback){
                    var content = {
                        plot_no: options.plot_no,
                        op_no: options.op_no,
                        machine_no: options.machine_no,
                        status: options.status
                    };

                    rollbackInfoGet(content, function(data){
                        var plotItem = data.plot_info;
                        plotItem.current_status = plotItem.status;
                        if(data.rollback_info && data.rollback_info.length > 0){
                            plotItem = angular.merge(plotItem, data.rollback_info[0]);
                            plotItem.machine_no = options.machine_no;
                            if (callback)
                                callback(plotItem);
                        } else {
                            $rootScope.showAlert('mobile05.msg.rollback_info_empty'); //查無報工還原資訊
                        }
                    });
                }

                function openDialog(plotItem){
                    $mdDialog.dialog(
                        'module/moduleMobile/program/mobile05/Rollback.tmp.html',
                        function (dialog) {
                            return {
                                plotItem: plotItem,
                                back: function(){
                                    dialog.hide();
                                },
                                confirm: function () {
                                    rollbackInfoProcess(dialog.plotItem, function(){
                                        dialog.hide();
                                        $rootScope.showToast($filter('translate')('mobile05.msg.success'));
                                        if(options.callback)
                                            options.callback();
                                    });
                                }
                            };
                        }
                    );
                } 
            
                beforeOpenDialog(options, function(plotItem){
                    openDialog(plotItem);
                });
            };
        }
    ]);
});
